package com.nitty.cash.loop.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonFragment
import com.nitty.cash.loop.configs.NittyConfigs
import com.nitty.cash.loop.databinding.UiPageTabMeBinding
import com.nitty.cash.loop.event.NittyEventData
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.ui.activity.FeedbackAct
import com.nitty.cash.loop.ui.activity.SettingAct
import com.nitty.cash.loop.ui.activity.UserProfileAct
import com.nitty.cash.loop.ui.activity.WebAct
import com.nitty.cash.loop.utils.NittyImageUtil
import com.nitty.cash.loop.utils.helper.NittyCustomDataHelper

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/12/10 14:53
 * @Annotation：我的
 */
class TabMeFragment: CommonFragment() {
    lateinit var binding: UiPageTabMeBinding
    override fun getNittyRootView(inflater: LayoutInflater): View {
        binding = UiPageTabMeBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
        setNittyTitleBarVisibility(View.VISIBLE)
        setNittyBackBtnVisibility(View.GONE)
        setNittyTextTitleVisibility(View.GONE)
        setNittyRightBtnRes(R.mipmap.img_customer_white)
        setNittyRootBackgroundLableRes(R.mipmap.nitty_withdraw_titlebar_bg)
        setNittyRootParentBackgroundColors(Color.parseColor("#ffffd451"), Color.parseColor("#ffffac17"))
        initMeRefrashView()
    }

    override fun initNittyListener() {
        binding.imgUserPicture.setOnClickListener {
            onIntent(UserProfileAct::class.java)
        }
        binding.textUserName.setOnClickListener {
            binding.imgUserPicture.callOnClick()
        }
        binding.profileLayout.setOnClickListener {
            binding.imgUserPicture.callOnClick()
        }

        binding.feedbackLayout.setOnClickListener {
            onIntent(FeedbackAct::class.java)
        }
        binding.privacyPolicyLayout.setOnClickListener {
            val showTitle = getString(R.string.privacy_policy_title)
            val linkUrl = NittyConfigs.NITTY_PRIVACY_POLICY_LINK
            WebAct.start(requireContext(),showTitle, linkUrl)
        }
        binding.settingLayout.setOnClickListener {
            onIntent(SettingAct::class.java)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun loadNittyData() {
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_USER_CENTER_START)
        //加载保存自定义数据
        NittyCustomDataHelper.getNittyCustomDataAction()
    }

    /* 初始化刷新 */
    private fun initMeRefrashView() {
        binding.meRefreshLayout.setColorSchemeResources(R.color.theme,R.color.purple_500)
        binding.meRefreshLayout.setOnRefreshListener {
            NittyCustomDataHelper.getNittyCustomDataAction()
        }
    }

    /**
     * 设置用户头像和昵称
     */
    private fun showNittyUserInfo(){
        val avatarUrl = NittyCustomDataHelper.getNittyCustomUserAvatarUrl()
        if (avatarUrl.isNotEmpty()){
            NittyImageUtil.instance.loadImageUrl(requireContext(),binding.imgUserPicture,avatarUrl)
        } else {
            binding.imgUserPicture.setImageResource(R.mipmap.img_holder)
        }
        binding.textUserName.text = NittyCustomDataHelper.getNittyCustomUserName()
    }

    override fun onNittyPublishEvent(event: NittyEventData) {
        super.onNittyPublishEvent(event)
        if (event.eventType == NittyEventData.TYPE.NITTY_CUSTOM_DATA_REFRESHED){
            hiddenNittyLoading()
            showNittyUserInfo()
        }
    }

    override fun hiddenNittyLoading() {
        super.hiddenNittyLoading()
        binding.meRefreshLayout.isRefreshing = false
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_USER_CENTER_END)
    }
}